home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / modules / saveilbm.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  6KB  |  198 lines

  1. /* saveilbm.c 04/92  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM save routines
  4.  *
  5.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  6.  *            for future compatibility
  7.  * 39.5 11/92 - raised BODYBUF size from 4096 to 5004 (should allow saving
  8.  *              of bitmaps up to pixel width of 16384)
  9.  */
  10. #define INTUI_V36_NAMES_ONLY
  11.  
  12. #include "iffp/ilbm.h"
  13. #include "iffp/ilbmapp.h"
  14.  
  15. extern struct Library *GfxBase;
  16.  
  17. /* screensave.c
  18.  *
  19.  * Given an ILBMInfo with a  currently available (not in use)
  20.  *   ParseInfo->iff IFFHandle, a screen pointer, filename, and
  21.  *   optional chunklist, will save screen as an ILBM
  22.  * The struct Chunk *chunklist1 and 2 are for chunks you wish written
  23.  * out other than BMHD, CMAP, and CAMG (they will be screened out
  24.  * because they are computed and written separately).
  25.  *
  26.  * Note -  screensave passes NULL for transparent color and mask
  27.  *
  28.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  29.  */
  30. LONG screensave(struct ILBMInfo *ilbm,
  31.             struct Screen *scr,
  32.             struct Chunk *chunklist1, struct Chunk *chunklist2,
  33.             UBYTE *filename)
  34.     {
  35.     extern struct Library *GfxBase;
  36.     Color32 *colortable32;
  37.     UWORD *colortable, count;
  38.     ULONG modeid;
  39.     LONG error;
  40.     int k;
  41.  
  42.     if(GfxBase->lib_Version >= 36)
  43.     modeid=GetVPModeID(&scr->ViewPort);
  44.     else
  45.     modeid = scr->ViewPort.Modes & OLDCAMGMASK;
  46.  
  47.     count = scr->ViewPort.ColorMap->Count;
  48.  
  49.     error = IFFERR_NOMEM;
  50.  
  51.     if(GfxBase->lib_Version >= 39)
  52.     {
  53.         if(colortable32 = (Color32 *)AllocMem(sizeof(Color32) * count, MEMF_CLEAR))
  54.         {
  55.         GetRGB32(scr->ViewPort.ColorMap,0L,count,(ULONG *)colortable32);
  56.             error = saveilbm(ilbm, scr->RastPort.BitMap, modeid,
  57.         scr->Width, scr->Height, scr->Width, scr->Height,
  58.         colortable32, count, 32,
  59.         mskNone, 0,
  60.         chunklist1, chunklist2, filename);
  61.         FreeMem(colortable32,sizeof(Color32) * count);
  62.         }
  63.     }
  64.     else
  65.     {
  66.         if(colortable = (UWORD *)AllocMem(count << 1, MEMF_CLEAR))
  67.         {
  68.         for(k=0; k<count; k++)
  69.         colortable[k]=GetRGB4(scr->ViewPort.ColorMap,k);
  70.  
  71.             error = saveilbm(ilbm, scr->RastPort.BitMap, modeid,
  72.         scr->Width, scr->Height, scr->Width, scr->Height,
  73.         colortable, count, 4,
  74.         mskNone, 0,
  75.         chunklist1, chunklist2, filename);
  76.         FreeMem(colortable,count << 1);
  77.         }
  78.     }
  79.     return(error);
  80.     }
  81.  
  82.  
  83. /* saveilbm
  84.  *
  85.  * Given an ILBMInfo with a currently available (not-in-use)
  86.  *   ParseInfo->iff IFFHandle, a BitMap ptr,
  87.  *   modeid, widths/heights, colortable, ncolors, bitspergun,
  88.  *   masking, transparent color, optional chunklists, and filename,
  89.  *   will save the bitmap as an ILBM.
  90.  *
  91.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  92.  *  if bitspergun=8,  colortable is byte guns of RGBRGB etc. (like a CMAP)
  93.  *  if bitspergun=32, colortable is ULONG guns of RGBRGB etc.
  94.  *     Only the high eight bits of each gun will be written to CMAP.
  95.  *     Four bit guns n will be saved as nn
  96.  *
  97.  * The struct Chunk *chunklist is for chunks you wish written
  98.  * other than BMHD, CMAP, and CAMG (they will be screened out)
  99.  * because they are calculated and written separately
  100.  *
  101.  * Returns 0 for success, or an IFFERR
  102.  */
  103. LONG saveilbm(struct ILBMInfo *ilbm,
  104.         struct BitMap *bitmap, ULONG modeid,
  105.         WORD width, WORD height, WORD pagewidth, WORD pageheight,
  106.         APTR colortable, UWORD ncolors, UWORD bitspergun,
  107.         WORD masking, WORD transparentColor,
  108.         struct Chunk *chunklist1, struct Chunk *chunklist2,
  109.         UBYTE *filename)
  110. {
  111. struct IFFHandle *iff;
  112. struct Chunk *chunk;
  113. ULONG chunkID;
  114. UBYTE *bodybuf;
  115. LONG size, error = 0L;
  116. #define BODYBUFSZ    5004
  117.  
  118.     iff = ilbm->ParseInfo.iff;
  119.  
  120.     if(!(modeid & 0xFFFF0000))    modeid &= OLDCAMGMASK;
  121.  
  122.     if(!(bodybuf = AllocMem(BODYBUFSZ,MEMF_PUBLIC)))
  123.     {
  124.     message(SI(MSG_IFFP_NOMEM));
  125.     return(IFFERR_NOMEM);
  126.     }
  127.  
  128.     if(!(error = openifile(ilbm, filename, IFFF_WRITE)))
  129.     {
  130.     D(bug("Opened %s for write\n",filename));
  131.  
  132.     error = PushChunk(iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN);
  133.  
  134.     D(bug("After PushChunk FORM ILBM - error = %ld\n", error));
  135.  
  136.         initbmhd(&ilbm->Bmhd, bitmap, masking, cmpByteRun1, transparentColor,
  137.                     width, height, pagewidth, pageheight, modeid);
  138.  
  139.     D(bug("Error before putbmhd = %ld\n",error));
  140.  
  141.     CkErr(putbmhd(iff,&ilbm->Bmhd));    
  142.  
  143.     if(colortable)    CkErr(putcmap(iff,colortable,ncolors,bitspergun));
  144.  
  145.  
  146.     if(ilbm->IFFPFlags & IFFPF_NOMONITOR) modeid &= (~MONITOR_ID_MASK);
  147.     ilbm->camg = modeid;
  148.     D(bug("before putcamg - error = %ld\n",error));
  149.     CkErr(putcamg(iff,&modeid));
  150.  
  151.     D(bug("Past putBMHD, CMAP, CAMG - error = %ld\n",error));
  152.  
  153.     /* Write out chunklists 1 & 2 (if any), except for
  154.      * any BMHD, CMAP, or CAMG (computed/written separately)
  155.      */
  156.     for(chunk = chunklist1; chunk; chunk = chunk->ch_Next)
  157.         {
  158.         D(bug("chunklist1 - have a %.4s\n",&chunk->ch_ID));
  159.         chunkID = chunk->ch_ID;
  160.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  161.         {
  162.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  163.             strlen(chunk->ch_Data) : chunk->ch_Size;
  164.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  165.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  166.         }
  167.         }
  168.  
  169.     for(chunk = chunklist2; chunk; chunk = chunk->ch_Next)
  170.         {
  171.         chunkID = chunk->ch_ID;
  172.         D(bug("chunklist2 - have a %.4s\n",&chunk->ch_ID));
  173.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  174.         {
  175.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  176.             strlen(chunk->ch_Data) : chunk->ch_Size;
  177.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  178.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  179.         }
  180.         }
  181.  
  182.     /* Write out the BODY
  183.      */
  184.     CkErr(putbody(iff, bitmap, NULL, &ilbm->Bmhd, bodybuf, BODYBUFSZ));
  185.  
  186.     D(bug("Past putbody - error = %ld\n",error));
  187.  
  188.  
  189.     CkErr(PopChunk(iff));    /* close out the FORM */
  190.     closeifile(ilbm);    /* and the file */
  191.     }
  192.  
  193.     FreeMem(bodybuf,BODYBUFSZ);
  194.  
  195.     return(error);
  196. }
  197.  
  198.